package com;

import java.util.ArrayList;
import java.util.Vector;

import de.dfki.util.xmlrpc.XmlRpc;

public class RPC implements RPCApi {

	private Peer p = null;

	public RPC(Peer p) {
		this.p = p;
	}

	public ArrayList<PeerData> query(int ttl) {
		ArrayList<PeerData> resultList = new ArrayList<PeerData>();
		RPCApi remote_api;

		// TTL Mechanism
		ttl++;
		if (ttl > 5)
			return resultList;

		// Query own peers
		ArrayList<PeerData> rpeers;
		for (PeerData r : p.peerList) {
			// Ping the peer
			remote_api = XmlRpc.createClient(RPCApi.class, "Peer", r.address,
					r.port);
			rpeers = remote_api.query(ttl);
			// Add his peers
			for (PeerData rp : rpeers) {
				if (!resultList.contains(rp))
					resultList.add(rp);
			}
		}

		// Add local peer data if not already in list.
		if (!resultList.contains(p.localPeerData))
			resultList.add(p.localPeerData);

		return resultList;
	}
}